
package com.elicitor.listner;

import com.elicitor.core.Alogrithm;
import com.elicitor.core.PraatCall;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;


public class elicitorgui extends javax.swing.JFrame {

    /** Creates new form elicitorgui */
    public elicitorgui() {
        initComponents();
        startFemale.setVisible(true);
        startMale.setVisible(true);
        recordingEnding.setVisible(false);
        emotionTesting.setVisible(false);
        emotionLabel.setVisible(false);
        emoAnalysis.setVisible(false);
    }
    
    MicRecorder mr;
    String fileLocation;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        startFemale = new javax.swing.JButton();
        startMale = new javax.swing.JButton();
        recordingEnding = new javax.swing.JButton();
        emotionTesting = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        emoAnalysis = new javax.swing.JButton();
        emotionLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        startFemale.setText("Start Male");
        startFemale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startFemaleActionPerformed(evt);
            }
        });

        startMale.setText("Start FeMale");
        startMale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startMaleActionPerformed(evt);
            }
        });

        recordingEnding.setText("End Recording..");
        recordingEnding.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                recordingEndingActionPerformed(evt);
            }
        });

        emotionTesting.setText("EmotionTesting");
        emotionTesting.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                emotionTestingActionPerformed(evt);
            }
        });

        jLabel1.setBackground(new java.awt.Color(240, 241, 242));
        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 60));
        jLabel1.setText("ELICITOR");

        emoAnalysis.setText("EmotionAnalyzing");
        emoAnalysis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                emoAnalysisActionPerformed(evt);
            }
        });

        emotionLabel.setFont(new java.awt.Font("Ubuntu", 1, 24)); // NOI18N
        emotionLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        emotionLabel.setText("jLabel2");
        emotionLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(86, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(82, 82, 82))
            .addGroup(layout.createSequentialGroup()
                .addGap(158, 158, 158)
                .addComponent(recordingEnding)
                .addContainerGap(161, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(emotionTesting)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 108, Short.MAX_VALUE)
                        .addComponent(emoAnalysis))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(startFemale)
                        .addGap(18, 18, 18)
                        .addComponent(emotionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                        .addComponent(startMale)))
                .addGap(36, 36, 36))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {startFemale, startMale});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel1)
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(startFemale)
                    .addComponent(startMale)
                    .addComponent(emotionLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addComponent(recordingEnding)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emoAnalysis)
                    .addComponent(emotionTesting))
                .addGap(32, 32, 32))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void startFemaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startFemaleActionPerformed

    mr = new MicRecorder(new AudioFormat(22000, 16, 2, true, true));
    mr.start();
    startFemale.setVisible(false);
    startMale.setVisible(false);
    emotionTesting.setVisible(false);
    recordingEnding.setVisible(true);
    emotionLabel.setVisible(false);
    emoAnalysis.setVisible(false);
    
    // TODO add your handling code here:
}//GEN-LAST:event_startFemaleActionPerformed

private void recordingEndingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_recordingEndingActionPerformed

    startFemale.setVisible(false);
    startMale.setVisible(false);
    recordingEnding.setVisible(false);
    emotionTesting.setVisible(true);
    emotionLabel.setVisible(false);
    emoAnalysis.setVisible(true);
    mr.stop();
    WaveData wd = new WaveData();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException ex) {
            Logger.getLogger(elicitorgui.class.getName()).log(Level.SEVERE, null, ex);
        }
    fileLocation=wd.saveToFile("clip", AudioFileFormat.Type.WAVE, mr.getAudioInputStream()); 
    
    // TODO add your handling code here:
}//GEN-LAST:event_recordingEndingActionPerformed

private void startMaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startMaleActionPerformed

    
    
}//GEN-LAST:event_startMaleActionPerformed

private void emotionTestingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emotionTestingActionPerformed

    PraatCall prattcall = new PraatCall(fileLocation);
    prattcall.createFile();
    prattcall.sendPraat();
    
}//GEN-LAST:event_emotionTestingActionPerformed

private void emoAnalysisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emoAnalysisActionPerformed

    Alogrithm algo = new Alogrithm("info.txt");
    String suitableEmotion = algo.detectEmotion();
    emotionLabel.setText(suitableEmotion);
    emotionLabel.setVisible(true);
    startFemale.setVisible(true);
        startMale.setVisible(true);
        recordingEnding.setVisible(false);
        emotionTesting.setVisible(false);
        emoAnalysis.setVisible(false);
    
    // TODO add your handling code here:
}//GEN-LAST:event_emoAnalysisActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(elicitorgui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(elicitorgui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(elicitorgui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(elicitorgui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new elicitorgui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton emoAnalysis;
    private javax.swing.JLabel emotionLabel;
    private javax.swing.JButton emotionTesting;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton recordingEnding;
    private javax.swing.JButton startFemale;
    private javax.swing.JButton startMale;
    // End of variables declaration//GEN-END:variables
}
